<template>
  <div class="login-container">
    <div class="login-form">
      <h1 class="title">椒江区人才社区地图管理系统</h1>
      <div class="form-title">用户名</div>
      <div class="form-content">
        <img class="icon" src="/src/assets/user.svg" alt="" />
        <input
          type="text"
          v-model="loginForm.username"
          placeholder="请输入用户名"
        />
      </div>
      <div class="form-title">密码</div>
      <div class="form-content">
        <img class="icon" src="/src/assets/lock.svg" alt="" />
        <input
          type="password"
          v-model="loginForm.password"
          placeholder="请输入密码"
        />
      </div>
      <div class="button-group">
        <button class="login" @click="handleLogin">立刻登录</button>
        <button class="reset" @click="handleReset">重置</button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { authAPI } from '@/api/index.js'
const router = useRouter()
const loading = ref(false)

const loginForm = reactive({
  username: '',
  password: '',
})

const handleLogin = async () => {
  try {
    // 验证表单
    if (!loginForm.username || !loginForm.password) {
      ElMessage.error('用户名和密码不能为空')
      return
    }
    loading.value = true
    // 调用登录API
    const response = await authAPI.login({
      username: loginForm.username,
      password: loginForm.password,
    })

    if (response.code === 200) {
      // 保存登录状态
      localStorage.setItem('token', response.data.token)
      localStorage.setItem('currentUser', JSON.stringify(response.data.user))
      localStorage.setItem('currentUsername', response.data.user.username)

      ElMessage.success(response.message || '登录成功！')

      // 跳转到首页
      await router.push('/home/data-boat')
    } else {
      ElMessage.error(response.message || '登录失败！')
    }
  } catch (error) {
    console.error('登录验证失败:', error)
    ElMessage.error('登录过程中发生错误')
  } finally {
    loading.value = false
  }
}

const handleReset = () => {
  loginForm.username = ''
  loginForm.password = ''
  loginFormRef.value?.clearValidate()
}
</script>

<style scoped lang="scss">
.login-container {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: url('/src/assets/login-bg.png') no-repeat;
  background-size: cover;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  box-sizing: border-box;
  margin: 0;
  overflow: hidden;
}

.login-form {
  margin-left: auto;
  margin-right: 120px;
  width: 600px;
  height: 640px;
  border-radius: 16px;
  background: #ffffffe5;
  border-radius: 12px;
  padding: 99px 66px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(20px);
  border: 2px solid #fff;
  transform: translateZ(0);
}

.title {
  font-family: PingFang SC;
  font-weight: 500;
  font-size: 35px;
  line-height: 100%;
  width: 100%;
  color: #323544;
  margin-bottom: 64px;
}

.form-title {
  font-family: Source Han Sans CN;
  font-weight: 400;
  font-size: 16px;
  line-height: 24px;
  color: #323333;
  margin-bottom: 12px;
}

.form-content {
  width: 100%;
  height: 56px;
  border: 1px solid #dcdee0;
  background: #ffffff;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px;
  border-radius: 8px;
  margin-bottom: 24px;
  .icon {
    width: 20px;
    height: 20px;
  }
  input {
    border: none;
    outline: none;
    width: 100%;
    height: 100%;
    font-size: 18px;
    &::placeholder {
      color: #b0b2b5;
      font-family: Source Han Sans CN;
      font-weight: 400;
      font-size: 16px;
      line-height: 24px;
    }
  }
}

.button-group {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-top: 64px;
  .login,
  .reset {
    cursor: pointer;
    width: 100%;
    height: 56px;
    border-radius: 8px;
    gap: 8px;
    line-height: 56px;
    text-align: center;
    border: none;
  }
  .login {
    background: linear-gradient(270deg, #44d4a8 0%, #30d1e6 100%);
    font-family: PingFang SC;
    font-weight: 500;
    font-style: Medium;
    font-size: 16px;
    color: #fff;
  }
  .reset {
    background: #dcdee0;
    font-family: Source Han Sans CN;
    font-weight: 400;
    font-style: Regular;
    font-size: 16px;
    color: #323333;
  }
}

/* 响应式设计 */
@media (max-width: 850px) {
  .login-container {
    padding: 10px;
  }

  .login-form {
    padding: 30px 20px;
    margin: 0;
    min-width: unset;
    max-width: 100%;
  }

  .title {
    font-size: 20px;
  }

  .button-group {
    flex-direction: column;
  }
}
</style>
